using System;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;

namespace LBSoft.Common.DotNetFile
{
    public class UploadHelper
    {
        public static string FileUpload(string path, FileUpload filleupload)
        {
            string result;
            try
            {
                bool fileOk = false;
                string fileExtension = Path.GetExtension(filleupload.FileName).ToLower();
                string[] allowExtension = new string[]
				{
					".rar",
					".zip",
					".rar",
					".ios",
					".jpg",
					".png",
					"bmp",
					".gif",
					".txt"
				};
                if (filleupload.HasFile)
                {
                    for (int i = 0; i < allowExtension.Length; i++)
                    {
                        if (fileExtension == allowExtension[i])
                        {
                            fileOk = true;
                            break;
                        }
                    }
                }
                if (fileOk)
                {
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    if (!FileHelper.IsExistFile(path + filleupload.FileName))
                    {
                        int Size = filleupload.PostedFile.ContentLength / 1024 / 1024;
                        if (Size > 10)
                        {
                            result = "上传失败,文件过大";
                        }
                        else
                        {
                            filleupload.PostedFile.SaveAs(path + filleupload.FileName);
                            result = "上传成功";
                        }
                    }
                    else
                    {
                        result = "上传失败,文件已存在";
                    }
                }
                else
                {
                    result = "不支持【" + fileExtension + "】文件格式";
                }
            }
            catch (Exception)
            {
                result = "上传失败";
            }
            return result;
        }

        /// <summary>
        /// 获取文件的真实类型(大写)，不受后缀名影响
        /// </summary>
        /// <param name="file">HttpPostedFile</param>
        /// <returns></returns>
        public static string GetFileRealType(HttpPostedFile file)
        {
            byte[] b = new byte[file.InputStream.Length];
            MemoryStream stream = new MemoryStream();
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = file.InputStream.Read(b, 0, b.Length)) > 0)
                {
                    ms.Write(b, 0, read);
                }
                stream = new MemoryStream(ms.ToArray());
            }
            System.IO.BinaryReader r = new System.IO.BinaryReader(stream);
            string bx = " ";
            byte buffer;
            try
            {
                buffer = r.ReadByte();
                bx = buffer.ToString();
                buffer = r.ReadByte();
                bx += buffer.ToString();
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
            }
            r.Close();
            return Enum.GetName(typeof(FileExtension), int.Parse(bx));
        }

        /// <summary>
        /// 获取文件的真实类型(大写)，不受后缀名影响
        /// </summary>
        /// <param name="stream">文件流</param>
        /// <returns></returns>
        public static string GetFileRealType(Stream stream)
        {
            System.IO.BinaryReader r = new System.IO.BinaryReader(stream);
            string bx = " ";
            byte buffer;
            try
            {
                buffer = r.ReadByte();
                bx = buffer.ToString();
                buffer = r.ReadByte();
                bx += buffer.ToString();
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
            }
            r.Close();
            return Enum.GetName(typeof(FileExtension), int.Parse(bx));
        }

        /// <summary>
        /// 文件格式对应编码枚举
        /// </summary>
        public enum FileExtension
        {
            JPG = 255216,
            //GIF = 7173,\
            ERR = 7173,//防止gif伪装图片，暂时如此处理
            BMP = 6677,
            PNG = 13780,
            COM = 7790,
            EXE = 7790,
            DLL = 7790,
            RAR = 8297,
            ZIP = 8075,
            XML = 6063,
            HTML = 6033,
            ASPX = 239187,
            CS = 117115,
            JS = 119105,
            TXT = 210187,
            SQL = 255254,
            BAT = 64101,
            BTSEED = 10056,
            RDP = 255254,
            PSD = 5666,
            PDF = 3780,
            CHM = 7384,
            LOG = 70105,
            REG = 8269,
            HLP = 6395,
            DOC = 208207,
            XLS = 208207,
            DOCX = 208207,
            XLSX = 208207,
        }

    }
}